﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Finance.Ioc
{
    public class IocManager
    {
        private static readonly IDictionary<string, Expression> FetcherCache = new Dictionary<string, Expression>();

        public static void Register<T>(Expression<Func<T>> fetcher) where T : class
        {
            if (!FetcherCache.ContainsKey(typeof (T).FullName))
            {
                FetcherCache[typeof (T).FullName] = fetcher;
            }
        }

        public static T Get<T>() where T : class
        {
            if (FetcherCache.ContainsKey(typeof (T).FullName))
            {
                return ((Expression<Func<T>>) FetcherCache[typeof (T).FullName]).Compile().Invoke();
            }
#if DEBUG
            throw new Exception("未注册"+typeof(T).FullName+"类型");
#endif
            return null;
        }
    }
}
